#include "server.h"

int main(int argc, const char *argv[])
{
	pid_t pid;
	int acceptfd;
	int sockfd = Open_server(sockfd);

	Creat_admin();

	signal(SIGCHLD, SIG_IGN);

	while(1){
		printf("+++++++++++++++++++++++++++++++++\n");
		if((acceptfd = accept(sockfd, NULL, NULL)) < 0){
			ERRLOG("accept failed!");
		}
        printf("acceptfd = %d\n", acceptfd);
		if((pid = fork()) < 0){
			ERRLOG("fork failed!");
			exit(-1);
		}
        if(pid == 0){
            printf("fork acceptfd >%d\n", acceptfd);
			Do_all(acceptfd);
		}else{
			close(acceptfd);
		}
	}

	return 0;
}

void Do_all(int acceptfd){
	MSG msg;
	printf("Do_all wait.....\n");
	int i = 0;
	while((i = recv(acceptfd, &msg, sizeof(MSG), 0)) > 0){
		printf("recv: ----%s\n", msg.name);
		switch(msg.type){
			case READ:
				Do_selectuser(acceptfd, &msg);  //OK
				break;
			case CHANGE:
				Do_modifyuser(acceptfd, &msg);
				break;
			case DELETE:
				Do_deluser(acceptfd, &msg);    //OK
				break;
			case ADD:
				Do_adduser(acceptfd, &msg);  //OK
				break;
			case LOAD:
				Do_login(acceptfd, &msg);   //OK
				break;
			case USERM:
				Do_user_modify(acceptfd, &msg);
				break;
			defalt:
				break;
		}
	}
	printf("Do_all over....\n");
}

int Open_server(int sockfd){
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
	if(sockfd < 0){
		ERRLOG("socket creat failed!");
	}

	struct sockaddr_in skdr;
	skdr.sin_family = AF_INET;
	skdr.sin_addr.s_addr = inet_addr(ADDR);
	skdr.sin_port = htons(PORT);

	if(bind(sockfd, (struct sockaddr *)&skdr, sizeof(skdr)) < 0){
		ERRLOG("bind failed!");
	}

	if(listen(sockfd, 5) < 0){
		ERRLOG("listen failed!");
	}

	return sockfd;
}

void Creat_admin(){
	FILE *fd;
	if((fd = fopen("users.txt", "r")) == NULL){

		USER user;
		USER userT;
		user.type = ADMIN;
		strcpy(user.name, "admin");
		strcpy(user.pwd, "admin");
		user.no = 1;
		
		FILE *fp = fopen("users.txt", "w+");
		if((fwrite(&user, sizeof(USER), 1, fp)) < 0){
			ERRLOG("fwrite error!");
		}
		fflush(fp);

		printf("creat success!\n");
	}
}

void Do_login(int acceptfd, MSG *msg){
	USER user;
	MSG msgnew;
	int flag = 0;
	strcpy(msgnew.name, msg->name);
	printf("login ----- %s ----- %s\n", msg->name, msgnew.name);
	FILE *fp = fopen("users.txt", "r");
	while(fread(&user, sizeof(user), 1, fp) > 0){
		if((strncmp(user.name, msg->name, strlen(user.name)) == 0)\
		   && (strncmp(user.pwd, msg->pwd, strlen(user.pwd)) == 0)){
		    msgnew.sign = SUCCESS;
			flag = 1;
			if(user.type == ADMIN){
				msgnew.sign = 999;
			}
			send(acceptfd, &msgnew, sizeof(msgnew), 0);
			break;
		}
	}
	if(flag == 0){
		msgnew.sign = FAILED;
		send(acceptfd, &msgnew, sizeof(msgnew), 0);
	}

	fclose(fp);
}

int Do_adduser(int acceptfd, MSG *msg){
	printf("adduser!\n");
	USER user, user_ex;
	int flag = 1;
	
	user.type = msg->info.type;
	user.no   = msg->info.no;
	strcpy(user.name, msg->info.name);
	strcpy(user.pwd , "123456");

	FILE *fp = fopen("users.txt", "a+");
	while(fread(&user_ex, sizeof(USER), 1,fp) > 0){
		if(strncmp(user.name, user_ex.name, strlen(user.name)) == 0){
			flag = 0;
			break;
		}
	}
	
	if(flag == 1){
	    fwrite(&user, sizeof(user), 1, fp);

		fclose(fp);

		FILE *fd = fopen("user_info.txt", "a+");
		fwrite(&(msg->info), sizeof(INFO), 1, fd);

		fclose(fd);
	}else if(flag == 0){
		fclose(fp);
	}else{
		//error
	}

	send(acceptfd, &flag, sizeof(int), 0);
	
	return 0;
}

int Do_deluser(int acceptfd, MSG *msg){
	int user_num, flag = 0;
	USER user_arr[100];
	USER user_tmp;
	INFO info_arr[100];
	INFO info_tmp;
	MSG msg_send;

	FILE *fa = fopen("users.txt", "r");
	int i = 0;
	while(fread(&user_tmp, sizeof(USER), 1, fa) > 0){
		if(strncmp(user_tmp.name, msg->name, strlen(user_tmp.name)) == 0){
			flag = 1;
			continue;
		}
		user_arr[i] = user_tmp;
		user_num = i++;
	}
	fclose(fa);

	if(flag == 1){
		FILE *fb = fopen("users.txt", "w+");
    	i = 0;
		while(i <= user_num){
			fwrite(&user_arr[i], sizeof(USER), 1, fb);
			i++;
		}
		fclose(fb);

		FILE *fc = fopen("user_info.txt", "r");
    	i = 0;
		while(fread(&info_tmp, sizeof(INFO), 1, fc) > 0){
			if(strncmp(info_tmp.name, msg->name, strlen(info_tmp.name)) == 0){
				continue;
			}
			info_arr[i] = info_tmp;
			user_num = i++;
		}
		fclose(fc);

		FILE *fd = fopen("user_info.txt", "w+");
		i = 0;
		while(i <= user_num){
			fwrite(&info_arr[i], sizeof(INFO), 1, fd);
			i++;
		}
		fclose(fd);
		strcpy(msg_send.data, "delete success!");
	}else{
		strcpy(msg_send.data, "delete failed!");
	}

	send(acceptfd, &msg_send, sizeof(MSG), 0);

	return 0;
}

int Do_modifyuser(int acceptfd, MSG *msg){
	INFO info_arr[100];
	INFO info_tmp;
	MSG msgnew;
	int i = 0, flag = 0, num;

	FILE *fa = fopen("user_info.txt", "r");
	while(fread(&info_tmp, sizeof(INFO), 1, fa) > 0){
		if(strncmp(info_tmp.name, msg->name, strlen(info_tmp.name)) == 0){
			flag = 1;
			info_tmp = msg->info;
		}
		info_arr[i] = info_tmp;
		num = i++;
	}
	fclose(fa);

	if(flag == 1){
		FILE *fb = fopen("user_info.txt", "w+");
		i = 0;
		while(i <= num){
			fwrite(&info_arr[i++], sizeof(INFO), 1, fb);
		}
		fclose(fb);
		strcpy(msgnew.data, "modify success!");
	}else if(flag == 0){
		strcpy(msgnew.data, "modify failed!");
	}else{
		strcpy(msgnew.data, "something error!");
	}

	send(acceptfd, &msgnew, sizeof(MSG), 0);

	return 0;
}

int Do_user_modify(int acceptfd, MSG *msg){
	USER user_arr[100];
	USER user_tmp;
	MSG msgnew;
	int i = 0, flag = 0, num;
	
	strcpy(msgnew.name, msg->name);

	FILE *fa = fopen("users.txt", "r");
	while(fread(&user_tmp, sizeof(USER), 1, fa) > 0){
		if(strncmp(user_tmp.name, msg->name, strlen(user_tmp.name)) == 0){
			flag = 1;
			strcpy(user_tmp.pwd, msg->data);
		}
		user_arr[i] = user_tmp;
		num = i++;
	}
	fclose(fa);

	if(flag == 1){
		FILE *fb = fopen("users.txt", "w+");
		i = 0;
		while(i <= num){
			fwrite(&user_arr[i++], sizeof(USER), 1, fb);
		}
		fclose(fb);
		strcpy(msgnew.data, "modify success!");
	}else if(flag == 0){
		strcpy(msgnew.data, "modify failed!");
	}
	send(acceptfd, &msgnew, sizeof(msgnew), 0);
	return 0;
}

int Do_selectuser(int acceptfd, MSG *msg){
	INFO info;
	MSG msgnew;
	int flag = 0;
	FILE *fp = fopen("user_info.txt", "r");
	while((fread(&info, sizeof(INFO), 1, fp)) > 0){
		if(strncmp(info.name, msg->name, strlen(info.name)) == 0){
			msgnew.sign = SUCCESS;
			msgnew.info = info;
			flag = 1;
			send(acceptfd, &msgnew, sizeof(MSG), 0);
			break;
		}
	}
	
	if(flag == 0){
		msgnew.sign = FAILED;
		send(acceptfd, &msgnew, sizeof(msgnew), 0);
	}
	
	return 0;
}














